--NOT TESTED, MAY BE INCORRECT
CREATE TABLE TRF_FREE_ACCOUNTS
(
 	   ACCOUNT_ID INTEGER PRIMARY KEY
);

CREATE TRIGGER TRF_INS_0N_ACC
ON TRF_ACCOUNT INSTEAD OF INSERT
AS
DECLARE @x MIN_FREE_ACCOUNT
BEGIN
	 IF @@ROWCOUNT=1
	 BEGIN
	 	IF (SELECT MAX(ACCOUNT_ID) FROM TRF_ACCOUNT)=9999999
	 	BEGIN
	 	  SELECT MIN(ACCOUNT_ID) INTO MIN_FREE_ACCOUNT FROM TRF_FREE_ACCOUNTS;
		  DELETE FROM TRF_FREE_ACCOUNTS WHERE ACCOUNT_ID = MIN_FREE_ACCOUNT;
		  INSERT INTO TRF_ACCOUNT VALUES(MIN_FREE_ACCOUNT,INSERTED.CLIENT_ID,INSERTED.TARIFF_ID,INSERTED.BALANCE);
	 	END
	    ELSE
		  BEGIN
	 	  	 INSERT INTO TRF_ACCOUNT VALUES(INSERTED.ACCOUNT_ID,INSERTED.CLIENT_ID,INSERTED.TARIFF_ID,INSERTED.BALANCE);
		  END
	 END	  
	 ELSE
	    BEGIN
            ROLLBACK TRAN;
            PRINT 'OPERATION IS NOT ALLOWED';
        END  	  
END;

CREATE TRIGGER TRF_DEL_0N_ACC
ON TRF_ACCOUNT FOR DELETE
AS
BEGIN
	 INSERT INTO TRF_FREE_ACCOUNTS(ACCOUNT_ID) VALUES(SELECT ACCOUNT_ID FROM DELETED);
END;